<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/commons/global.jsp" %>
<style>
.easyui-text{
	width: 80px; height: 25px;border-radius:5px;border:thin solid #ddd;
	border-color:#95B8E7;
}
.easyui-text:focus{
	outline: none;
}
</style>
<script type="text/javascript">
    $(function() {
    	 $('[name=name]').blur(function(){
    		 var val=$(this).val();
    		 if(val!=''){
	             $.ajax({
	                 url:basePath+'/generator/valid/'+val,
	                 type:'post',
	                 dataType:'json',
	                 success:function(result){
	                     if(result.success){
	                         $.messager.alert('提示',result.msg, 'warning');
	                     }
	                 }
	             })
    		 }
         });
        $('#add').click(function(){
            $('#table tbody').append('<tr>'
            		 +'<td><input type="text" name="field" class="easyui-text"'
            		 +' placeholder="请输入列名"></td>'
            		 +'<td><select name="type" class="easyui-text" data-options="width:80,height:29,editable:false,panelHeight:\'auto\'">'
            		 +' <option value=""></option>'
            		 +'<option value="int">int</option>'
            		 +'<option value="varchar">varchar</option>'
            		 +'<option value="text">text</option>'
            		 +'<option value="bigint">bigint</option>'
            		 +' <option value="tinyint">tinyint</option>'
            		 +' <option value="date">date</option>'
            		 +'<option value="datetime">datetime</option>'
            		 +' </select></td>'
            		 +' <td><input type="text" name="length" class="easyui-text">'
            		 +' </td>'
            		 +'<td><input type="text" name="defaultValue"'
            		 +'         class="easyui-text"></td>'
            		 +' <td>'
            		 +' <input type="checkbox" name="key" value="1" class="key">'
            		 +'</td>'
            		 +' <td>'
                     +'<input type="checkbox" name="isNull" value="1" class="isNull">'
                     +' </td>'
                     +' <td>'
                     +'<select name="extra" class="easyui-text" >'
                     +'<option value="-1">请选择</option>'
                     +'<option value="auto_increment">自增</option>'
                     +'<option value="UUID">UUID</option>'
                     +' </select>'
                     +'</td>'
                     +'<td><input type="text" name="comment" class="easyui-text"></td>'
                     +'<td>'
                     +'<input type="checkbox" name="isTransient" value="1" >'
                     +'</td>'
                     +'<td><select name="showType" class="easyui-text" data-options="width:80,height:29,editable:false,panelHeight:\'auto\'">'
                     +'<option value="hidden">隐藏域</option>'
                     +' <option value="text" selected>文本</option>'
                     +'<option value="date">日期</option>'
                     +'<option value="datetime">日期时间</option>'
                     +' <option value="textarea">大文本</option>'
                     +'<option value="radio">单选</option>'
                     +'<option value="checkbox">多选</option>'
                     +' <option value="numbere">数字</option>'
                     +'<option value="email">邮箱</option>'
                     +' <option value="tel">电话</option>'
                     +'<option value="select">下拉选择</option>'
                     +'<option value="select2">select2</option>'
                     +'</select></td>'
                     +' <td>'
                     +' <input type="checkbox" name="isQuery" value="1">'
                     +' </td>'
                     +' <td>'
                     +' <input type="checkbox" name="isLike" value="1">'
                     +' </td>'
                     +' <td>'
                     +' <input type="input" name="seq" class="easyui-text" value="0">'
                     +' </td>'
                     +' <td>'
                     +'<select name="valid" class="easyui-text" data-options="width:80,height:29,editable:false,panelHeight:\'auto\'">'
                     +'<option value="" selected></option>'
                     +' <option value="required:true">必填</option>'
                     +'</select>'
                     +'</td>'
                     +'<td>'
                     +' <button class="easyui-linkbutton del">删除</button>'
                     +' <button class="easyui-linkbutton up " >上移</button>'
                     +' <button class="easyui-linkbutton down " >下移</button>'
                     +'</td>'
                     +' </tr>');
           
            $('.easyui-linkbutton').linkbutton();
            return false;
        });
        //删除
        $('#table tbody').on('click','.del',function(){
            var $this=$(this);
            $this.closest('tr').remove();
            return false;
        })
        //上移
        $('#table tbody').on('click','.up',function(){
            var current = $(this).parent().parent(); //获取当前<tr>
            var prev = current.prev();  //获取当前<tr>前一个元素
            if (current.index() > 0) {
                current.insertBefore(prev); //插入到当前<tr>前一个元素前
            }
            return false;
        })
        //下移
        $('#table tbody').on('click','.down',function(){
            var current = $(this).parent().parent(); //获取当前<tr>
            var next = current.next(); //获取当前<tr>后面一个元素
            if (next) {
                current.insertAfter(next);  //插入到当前<tr>后面一个元素后面
            }
            return false;
        })

        $('#table').on('click',' .key',function(){
        	if($(this).is(':checked')){
        		 $(this).parent().next().find('.isNull').attr('checked',true);
        	}else{
                $(this).parent().next().find('.isNull').attr('checked',false);
            }
        });
        
    });

    function submit(){
    	 var tableName=$('[name=name]').val();
         var description=$('#description').val();
         var isTree=false;
         if($('#isTree').is(':checked')){
        	 isTree=true;
         }
         if(tableName==""){
             $.messager.alert('提示','表名不能为空','warning');
             $('[name=name]').focus();
         }else{
             var fields=new Array();
             var types=new Array();
             var lengths=new Array();
             var defaultValues=new Array();
             var keys=new Array();
             var isNulls=new Array();
             var extras=new Array();
             var comments=new Array();
             var showTypes=new Array();
             var isTransients=new Array();
             var isQuerys=new Array();
             var isLikes=new Array();
             var seqs=new Array();
             var valids=new Array();
             $('[name=field]').each(function(){
                 fields.push($(this).val());
             })
             $('[name=seq]').each(function(){
                 seqs.push($(this).val());
             })
             $('[name=type]').each(function(){
                 types.push($(this).val());
             })
             $('[name=length]').each(function(){
                 lengths.push($(this).val()==""?"0":$(this).val());
             })
             $('[name=defaultValue]').each(function(){
                 defaultValues.push($(this).val()==""?"0":$(this).val());
             })
             $('[name=key]').each(function(){
                 if($(this).is(':checked')){
                     keys.push("1");
                 }else{
                     keys.push("0");
                 }

             })
             $('[name=isNull]').each(function(){
                 if($(this).is(':checked')){
                     isNulls.push("1");
                 }else{
                     isNulls.push("0");
                 }
             })
             $('[name=extra]').each(function(){
            	   extras.push($(this).val());
             })
             $('[name=isTransient]').each(function(){
                 if($(this).is(':checked')){
                     isTransients.push("1");
                 }else{
                     isTransients.push("0");
                 }
             })
             $('[name=isQuery]').each(function(){
                 if($(this).is(':checked')){
                     isQuerys.push("1");
                 }else{
                     isQuerys.push("0");
                 }
             })
             $('[name=isLike]').each(function(){
                 if($(this).is(':checked')){
                     isLikes.push("1");
                 }else{
                     isLikes.push("0");
                 }
             })
             $('[name=valid] option:selected').each(function(){
                 valids.push($(this).val()==""?"0":$(this).val());
             })
             $('[name=showType] option:selected').each(function(){
                 showTypes.push($(this).val());
             })
             $('[name=comment]').each(function(){
                 comments.push($(this).val());
             })

             var i=0,j=0,k=0,l=0,n=0;
             $.each(fields,function(index,item){
                 if(item==""){
                     i++;
                 }
             })
             $.each(keys,function(index,item){
                 console.log(item==0)
                 l+=item;
             })
             $.each(types,function(index,item){
                 if(item==""){
                     j++;
                 }
             })
             $.each(comments,function(index,item){
                 if(item==""){
                     k++;
                 }
             })
             $.each(showTypes,function(index,item){
                 if(item==""){
                     n++;
                 }
             })
             if(n>0){
                 msg+="【页面显示方式不能为空】";
             }
             var msg=""
             if(i>0){
                 msg+="【列名不能为空】";
             }
             if(j>0){
                 msg+=" 【列字段数据类型不能为空】";
             }
             // if(k>0){
             //     msg+=" 【列注释不能为空】";
             // }
             if(l==0){
                 msg+="【表"+tableName+"没有设置主键】";
             }
             if(msg!=""){
            	 $.messager.alert('提示',msg,'warning');
                 return false;
             }
             var data={"tableName":tableName,"description":description,"fields":fields.join(","),"types":types.join(","),"lengths":lengths.join(","),"defaultValues":defaultValues.join(","),
                 "keys":keys.join(","),"isNulls":isNulls.join(","),"extras":extras.join(","),"comments":comments.join(","),"isQuerys":isQuerys.join(","),"isTransients":isTransients.join(","),
                 "showTypes":showTypes.join(","),"valids":valids.join(','),"isTree":isTree,"seqs":seqs.join(','),"isLikes":isLikes.join(",")};
             console.log(data)
             $.ajax({
                 url:basePath+'/generator/addOrEdit',
                 type:'post',
                 data:data,
                 dataType:'json',
                 success:function(result){
                     result=eval('(result)');
                     showMsg("添加成功");
                     parent.$.modalDialog.openner_dataGrid.datagrid('reload');//之所以能在这里调用到parent.$.modalDialog.openner_dataGrid这个对象，是因为user.jsp页面预定义好了
                     parent.$.modalDialog.handler.dialog('close');
                 },error:function(error){
                     $.messager.alert('添加出错！可能是sql语句语法不对！！','error');
                 }
             })
         }
         return false;
       }
</script>
<div class="easyui-layout" data-options="fit:true,border:false" >
    <div data-options="region:'center',border:false" style="overflow-x:hidden;overflow-y: auto;padding: 3px;" >
        <form id="codeGeneratorAddForm" method="post">
            <table class="grid">
            <tr>
                     <td style="width:10%">表名</td>
                      <td style="width:25%">
                        <input name='name' id='name' style="width:80%" type='text' placeholder='请输入表名' class='easyui-text span2 easyui-validatebox' data-options="required:false">
                     </td>
                       <td style="width:10%">描述</td>
                       <td style="width:25%">
                        <input name='description' id='description' style="width:80%"  type='text' placeholder='请输入描述' class='easyui-text  span2' >
                     </td>
                     <td style="width:15%">生成树表</td>
                     <td style="width:15%">
                     	<input type="checkbox" id="isTree"/>
                     </td>
                  </tr> 
            </table>
        </form>
        <button id="add" class="easyui-linkbutton" style="float: right;margin:10px 10px 10px 100px;" data-options="iconCls:'fi-plus icon-green'">添加一行</button>
        <table class="grid" id="table">
        	 <thead>
            <tr>
                <th>列名<span style="color:red">*</span></th>
                <th>数据类型<span style="color:red">*</span></th>
                <th>长度</th>
                <th>默认值</th>
                <th>主键</th>
                <th>非空</th>
                <th>主键策略</th>
                <th>注释</th>
                <th>临时对象</th>
                <th>页面显示方式<span style="color:red">*</span></th>
                <th>是否是查询参数</th>
                <th>使用Like查询</th>
                <th>排序</th>
                <th>校验</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            <tr>
                <td><input type="text" name="field" class="easyui-text"
                           placeholder="请输入列名"></td>
                <td><select name="type" class="easyui-text" data-options="width:80,height:29,editable:false,panelHeight:'auto'">
                    <option value=""></option>
                    <option value="int">int</option>
                    <option value="varchar">varchar</option>
                    <option value="text">text</option>
                    <option value="bigint">bigint</option>
                    <option value="tinyint">tinyint</option>
                    <option value="date">date</option>
                    <option value="datetime">datetime</option>
                </select></td>
                <td><input type="text" name="length" class="easyui-text">
                </td>
                <td><input type="text" name="defaultValue"
                           class="easyui-text"></td>
                <td>
                    <input type="checkbox" name="key" value="1" class="key">
                </td>
                <td>
                    <input type="checkbox" name="isNull" value="1" class="isNull">
                </td>
                <td>
                    <select name="extra" class="easyui-text" >
                    	<option value="-1">请选择</option>
                    	<option value="auto_increment">自增</option>
                    	<option value="UUID">UUID</option>
                    </select>
                </td>
                <td><input type="text" name="comment" class="easyui-text"></td>
                <td>
                    <input type="checkbox" name="isTransient" value="1" >
                </td>
                <td><select name="showType" class="easyui-text" data-options="width:80,height:29,editable:false,panelHeight:'auto'">
                    <option value="hidden">隐藏域</option>
                    <option value="text" selected>文本</option>
                    <option value="date">日期</option>
                    <option value="datetime">日期时间</option>
                    <option value="textarea">大文本</option>
                    <option value="radio">单选</option>
                    <option value="checkbox">多选</option>
                    <option value="numbere">数字</option>
                    <option value="email">邮箱</option>
                    <option value="tel">电话</option>
                    <option value="select">下拉选择</option>
                    <option value="select2">select2</option>
                </select></td>
                <td>
                    <input type="checkbox" name="isQuery" value="1">
                </td>
                <td>
                    <input type="checkbox" name="isLike" value="1">
                </td>
                <td>
                    <input type="input" name="seq" value="0" class="easyui-text">
                </td>
                <td>
                    <select name="valid" class="easyui-text" data-options="width:80,height:29,editable:false,panelHeight:'auto'">
                        <option value="" selected></option>
                        <option value="required:true">必填</option>
                    </select>
                </td>
                <td>
                    <button class="easyui-linkbutton del">删除</button>
                    <button class="easyui-linkbutton up " >上移</button>
                    <button class="easyui-linkbutton down " >下移</button>
                </td>
            </tr>
            </tbody>
        </table>
    </div>
</div>